package com.aura.common.domain.domain;

import lombok.Data;

/**
 * 通用返回结构体
 */
@Data
public class R<T> {

    private int code;
    private String message;
    private T data;

    // === 成功 ===

    public static <T> R<T> ok() {
        return restResult(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data, String message) {
        return restResult(data, ResultCode.SUCCESS.getCode(), message);
    }

    // === 失败 ===

    public static <T> R<T> fail() {
        return restResult(null, ResultCode.ERROR);
    }

    public static <T> R<T> fail(String message) {
        return restResult(null, ResultCode.ERROR.getCode(), message);
    }

    public static <T> R<T> fail(int code, String message) {
        return restResult(null, code, message);
    }

    public static <T> R<T> fail(ResultCode code) {
        return restResult(null, code);
    }

    public static <T> R<T> fail(T data, ResultCode code) {
        return restResult(data, code);
    }

    // === 内部构建 ===

    private static <T> R<T> restResult(T data, ResultCode code) {
        return restResult(data, code.getCode(), code.getMessage());
    }

    private static <T> R<T> restResult(T data, int code, String message) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    // === 成功判断 ===
    public boolean isSuccess() {
        return this.code == ResultCode.SUCCESS.getCode();
    }
}
